//
//  DingListCell.swift
//  JianTingSwift
//
//  Created by zhibao on 2024/11/22.
//

import UIKit

@objc class DingListCell: UITableViewCell {
    var imgView:UIImageView!
    var newIconView:UIImageView!
    var nameLabel:UILabel!
    var priceLabel:UILabel!
    var dayAndNumLabel:UILabel!
    var baoYouLabel:UILabel!
    var zongLabel:UILabel!
    var timeLabel:UILabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        
        let bgview = UIView()
        bgview.backgroundColor = MYColor.color(withHexString: "f2f2f2")
        self.contentView.addSubview(bgview)
        bgview.layer.cornerRadius = pixw(p:15)
        bgview.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15));
            make.top.equalTo(pixw(p:10));
            make.right.equalTo(pixw(p:-15));
            make.bottom.equalTo(0);
        }
        
        imgView = UIImageView(image: UIImage(named: "h_project"))
        imgView.contentMode = .scaleAspectFit
        bgview.addSubview(imgView)
        imgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 20))
            make.centerY.equalTo(bgview)
            make.width.height.equalTo(pixw(p: 100))
        }
        
        newIconView = UIImageView(image: UIImage(named: "h_5"))
        imgView.addSubview(newIconView)
        newIconView.snp.makeConstraints { make in
            make.left.top.equalTo(imgView)
            make.width.equalTo(pixw(p: 50))
            make.height.equalTo(pixw(p: 18))
        }
        
        nameLabel = UILabel()
        nameLabel.text = "荣耀手环5 陨石黑 智能 运动"
        nameLabel.font = MY_SCALE_MediumFONT(15)
        nameLabel.numberOfLines = 2
        nameLabel.lineBreakMode = .byWordWrapping
        nameLabel.textColor = MYBlock
        bgview.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(pixw(p: 10));
            make.top.equalTo(pixw(p: 10));
            make.right.equalTo(bgview).offset(pixw(p:-15));
        }
        
        
        let qianPriceLabel = UILabel()
        qianPriceLabel.text = "¥"
        qianPriceLabel.font = MY_SCALE_LightFONT(12)
        qianPriceLabel.textColor = MYRed
        bgview.addSubview(qianPriceLabel)
        qianPriceLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p: 10));
        }
        
        priceLabel = UILabel()
        priceLabel.text = "27"
        priceLabel.font = MY_SCALE_BoldFont(20)
        priceLabel.textColor = MYRed
        bgview.addSubview(priceLabel)
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(qianPriceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        let cpriceLabel = UILabel()
        cpriceLabel.text = "/天"
        cpriceLabel.font = MY_SCALE_LightFONT(12)
        cpriceLabel.textColor = MYRed
        bgview.addSubview(cpriceLabel)
        cpriceLabel.snp.makeConstraints { make in
            make.left.equalTo(priceLabel.snp.right).offset(0);
            make.bottom.equalTo(qianPriceLabel);
        }
        
        baoYouLabel = UILabel()
        baoYouLabel.text = "   vip独享:包邮包送   "
        baoYouLabel.font = MY_SCALE_RegularFONT(11)
        baoYouLabel.layer.cornerRadius = pixw(p: 3)
        baoYouLabel.layer.masksToBounds = true
        baoYouLabel.backgroundColor = MYColor.color(withHexString: "#E95A27", alpha: 0.1)
        baoYouLabel.textColor = MYRed
        bgview.addSubview(baoYouLabel)
        baoYouLabel.snp.makeConstraints { make in
            make.left.equalTo(cpriceLabel.snp.right).offset(pixw(p: 2));
            make.centerY.equalTo(cpriceLabel);
            make.height.equalTo(pixw(p: 18))
        }
        
        dayAndNumLabel = UILabel()
        dayAndNumLabel.text = "天数：0天  数量：10台"
        dayAndNumLabel.font = MY_SCALE_RegularFONT(12)
        dayAndNumLabel.textColor = MYColor.color(withHexString: "#666666")
        bgview.addSubview(dayAndNumLabel)
        dayAndNumLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(qianPriceLabel.snp.bottom).offset(pixw(p: 10))
        }
        
        let czongLabel = UILabel()
        czongLabel.text = "总计:"
        czongLabel.font = MY_SCALE_LightFONT(13)
        czongLabel.textColor = MYRed
        bgview.addSubview(czongLabel)
        czongLabel.snp.makeConstraints { make in
            make.left.equalTo(dayAndNumLabel);
            make.top.equalTo(dayAndNumLabel.snp.bottom).offset(pixw(p: 10))
        }
        
        zongLabel = UILabel()
        zongLabel.text = "300.00"
        zongLabel.font = MY_SCALE_BoldFont(17)
        zongLabel.textColor = MYRed
        bgview.addSubview(zongLabel)
        zongLabel.snp.makeConstraints { make in
            make.left.equalTo(czongLabel.snp.right);
            make.bottom.equalTo(czongLabel)
        }
        
        let yuanLabel = UILabel()
        yuanLabel.text = "元"
        yuanLabel.font = MY_SCALE_LightFONT(13)
        yuanLabel.textColor = MYRed
        bgview.addSubview(yuanLabel)
        yuanLabel.snp.makeConstraints { make in
            make.left.equalTo(zongLabel.snp.right);
            make.bottom.equalTo(czongLabel)
        }
        
        
        timeLabel = UILabel()
        timeLabel.text = "时间: 2022-10-23 10:10:10"
        timeLabel.font = MY_SCALE_RegularFONT(12)
        timeLabel.textColor = MYColor.color(withHexString: "#999999")
        bgview.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(czongLabel.snp.bottom).offset(pixw(p: 10))
            make.bottom.equalTo(pixw(p: -10))
        }
        
        let stateLabel = UILabel()
        stateLabel.text = "待审核"
        stateLabel.font = MY_SCALE_LightFONT(18)
        stateLabel.textColor = MYRed
        bgview.addSubview(stateLabel)
        stateLabel.snp.makeConstraints { make in
            make.right.equalTo(pixw(p: -20))
            make.centerY.equalTo(czongLabel.snp.bottom)
        }
        
    }
    
    /// 更新数据
    /// - Parameters:
    ///   - name: 名字带后缀
    ///   - docum: 路径
    ///   - type: 类型
    func updata(dic:Any) {
        let superDic = dic as! [String:Any]
        let proDic = superDic["product"] as! [String:String]
        nameLabel.text = proDic["name"]
        priceLabel.text = proDic["price"]
        dayAndNumLabel.text = "天数：\(superDic["dayNum"] ?? "")天  数量：\(superDic["shuNum"] ?? "")台"
        baoYouLabel.text = "   vip独享:\(proDic["vip"] ?? "")   "
        imgView.image = UIImage(named: proDic["img"] ?? "")
        newIconView.image = UIImage(named: proDic["new"] ?? "")
        if proDic["new"] == "" {
            newIconView.isHidden = true
        }else{
            newIconView.isHidden = false
        }
        timeLabel.text = "时间: \(superDic["time"] ?? "")"
        zongLabel.text = "300"
        if let day = Int(superDic["dayNum"] as! String) {
            print("转换成功: \(day)")
            if let num = Int(superDic["shuNum"] as! String) {
                print("转换成功: \(num)")
                if let price = Int(proDic["price"]!) {
                    print("转换成功: \(price)")
                    zongLabel.text = "\(day * num * price).00"
                } else {
                    print("转换失败: 字符串不是有效的整数")
                }
            } else {
                print("转换失败: 字符串不是有效的整数")
            }
        } else {
            print("转换失败: 字符串不是有效的整数")
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

